package com.htbb.base.handler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.TypeMismatchException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ResponseBody;

import com.htbb.base.ReturnBean;
import com.htbb.base.exception.ErrorCodeException;





/**
 * 
 * @author ludexiang
 *
 */
@ControllerAdvice
public class ExceptionHandler {
    static Logger log = LoggerFactory.getLogger(ExceptionHandler.class);

	@org.springframework.web.bind.annotation.ExceptionHandler(Exception.class)
    @ResponseBody
    public ResponseEntity<ReturnBean> defaultErrorHandler(HttpServletRequest request, HttpServletResponse response, Exception e) {
    	ReturnBean r = new ReturnBean();
    	r.setCode(999); 
    	r.setResultMsg("Unknown Exception");
        log.error("Unknown Exception", e);
		return new ResponseEntity<ReturnBean>(r, HttpStatus.SERVICE_UNAVAILABLE);
    }

    @org.springframework.web.bind.annotation.ExceptionHandler(TypeMismatchException.class)
    @ResponseBody
    public ResponseEntity<ReturnBean> typeMismatchExceptionHandler(HttpServletRequest request, HttpServletResponse response, TypeMismatchException e) {
    	ReturnBean r = new ReturnBean();
    	r.setCode(998);
    	r.setResultMsg("参数类型错误：" +e.getMessage());
        log.error("参数类型错误：", e);
		return new ResponseEntity<ReturnBean>(r, HttpStatus.SERVICE_UNAVAILABLE);
    }

    @org.springframework.web.bind.annotation.ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    @ResponseBody
    public ResponseEntity<ReturnBean> httpRequestMethodNotSupportedExceptionHandler(HttpServletRequest request, HttpServletResponse response, HttpRequestMethodNotSupportedException e) {
    	ReturnBean r = new ReturnBean();
    	r.setCode(999);
    	r.setResultMsg("不支持该方法："+e.getMessage());
        log.error("不支持该方法：", e);
		return new ResponseEntity<ReturnBean>(r, HttpStatus.SERVICE_UNAVAILABLE);
    }

    @org.springframework.web.bind.annotation.ExceptionHandler(MissingServletRequestParameterException.class)
    @ResponseBody
    public ResponseEntity<ReturnBean> missingServletRequestParameterExceptionExceptionHandler(HttpServletRequest request, HttpServletResponse response, MissingServletRequestParameterException e) {
    	ReturnBean r = new ReturnBean();
    	r.setCode(999);
    	r.setResultMsg("参数未找到："+e.getMessage());
        log.error("参数未找到：", e);
		return new ResponseEntity<ReturnBean>(r, HttpStatus.SERVICE_UNAVAILABLE);
    }
    
    @org.springframework.web.bind.annotation.ExceptionHandler(ErrorCodeException.class)
    @ResponseBody
    public ResponseEntity<ReturnBean> errorCodeExceptionErrorHandler(HttpServletRequest request, HttpServletResponse response, ErrorCodeException e) {
        ReturnBean r = new ReturnBean();
        r.setCode(e.getCode());
    	r.setResultMsg(e.getMsg());
        log.error("错误参数：", e);
		return new ResponseEntity<ReturnBean>(r, HttpStatus.SERVICE_UNAVAILABLE);
    }
}
